package com.venusj.singleton;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author zhangjh
 * @date 2020/12/25
 * @desc 通过序列化和反序列化 破坏单例机制
 */
public class MySingletonTest2 {


    public static void main(String[] args) throws IOException, ClassNotFoundException {
        MySingleton mySingleton = MySingleton.getInstance();

        ObjectOutputStream oos = null;

        oos = new ObjectOutputStream(new FileOutputStream("D:\\code\\edu-note\\design-model\\tempFile.txt"));
        oos.writeObject(mySingleton);

        File file = new File("D:\\code\\edu-note\\design-model\\tempFile.txt");


        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
        MySingleton o = (MySingleton) objectInputStream.readObject();

        System.out.println(mySingleton);
        System.out.println(o);
        System.out.println(o == mySingleton);
    }
}
